import fs from "fs";
let time = require('silly-datetime');
import path from "path";
import chalk from "chalk";
const LogPath = path.join(__dirname, '../../../logger');
const errorLogFile = path.join(LogPath, 'error.log');
const otherLogFile = path.join(LogPath, 'error.log');
const env = process.env.NODE_ENV ? process.env.NODE_ENV : "dev";


class LogsModel {
    constructor() {
        if (!fs.existsSync(LogPath)) {
            fs.mkdirSync(LogPath);
            console.log("write LogPath ok!");
        };
        if (!fs.existsSync(errorLogFile)) {
            fs.writeFileSync(errorLogFile, "", { encoding: 'utf-8' });
            console.log("write errorLogFile ok!");
        };
        if (!fs.existsSync(otherLogFile)) {
            fs.writeFileSync(otherLogFile, "", { encoding: 'utf-8' });
            console.log("write errorLogFile ok!");
        };
    };
    async catchError(data) {
        if (env == "dev") {
            console.log(chalk.red.bgWhite(`[DEBUG][ERROR]:${data}`));
        }
        return new Promise((resolv, reject) => {
            fs.open(errorLogFile, "a", (err, fd) => {
                if (!err) {
                    fs.write(fd, time.format(new Date(), 'YYYY-MM-DD HH:mm ') + data + "\n", (err, result) => {
                        if (!err) {

                            fs.close(fd, err => {
                                if (!err) {
                                    resolv("ok")
                                } else {
                                    reject(err)
                                }
                            })
                        } else {
                            reject(err)
                        }
                    })
                } else {
                    reject(err)
                }
            })
        })
    };
    async log(data) {
        if (env == "dev") {
            console.log(chalk.blue.bgWhite(`[DEBUG][INFO]:${data}`));
        }
        return new Promise((resolv, reject) => {
            fs.open(errorLogFile, "a", (err, fd) => {
                if (!err) {
                    fs.write(fd, time.format(new Date(), 'YYYY-MM-DD HH:mm ') + data + "\n", (err, result) => {
                        if (!err) {
                            fs.close(fd, err => {
                                if (!err) {
                                    resolv("ok")
                                } else {
                                    reject(err)
                                }
                            })
                        } else {
                            reject(err)
                        }
                    })
                } else {
                    reject(err)
                }
            })
        })
    };
    async saveLog(data?: any, doing?: any) {
        if (env == "dev") {
            console.log(chalk.yellow.bgWhite(`[DEBUG][INFO]:${data}`));
        }
        return new Promise((resolv, reject) => {
            fs.open(errorLogFile, "a", (err, fd) => {
                if (!err) {
                    fs.write(fd, time.format(new Date(), 'YYYY-MM-DD HH:mm ') + data + "\n", (err, result) => {
                        if (!err) {

                            fs.close(fd, err => {
                                if (!err) {
                                    resolv("ok")
                                } else {
                                    reject(err)
                                }
                            })
                        } else {
                            reject(err)
                        }
                    })
                } else {
                    reject(err)
                }
            })
        })
    }
}

const Logs = new LogsModel();
export default Logs;